import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import './res/listData.dart';

void main() {
  runApp(
    MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: const Text('你好flutter')),
        body: const MyApp(),
      ),
    ),
  );
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});
  
  Widget _initData(content, index) {
    return Container(
      decoration: BoxDecoration(
        color: Colors.teal[50],
      ),
      child: Column(
        children: [
          Image.network(listData[index]['imageUrl']),
          const SizedBox(height: 10),
          Text(
            listData[index]['title'],
            style: const TextStyle(fontSize: 18),
          )
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    // TODO 构造器
    return GridView.builder(
      itemCount: listData.length,

      // 类似GridView.count
      gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
        crossAxisCount: 2, // 多少列
        crossAxisSpacing: 10, // 左右间距
        mainAxisSpacing: 10, // 上下间距
        childAspectRatio: 1, // 宽高比
      ),

      // 类似GridView.extent
      // gridDelegate: const SliverGridDelegateWithMaxCrossAxisExtent(
      //   maxCrossAxisExtent: 200, //
      //   crossAxisSpacing: 10, // 左右间距
      //   mainAxisSpacing: 10, // 上下间距
      //   childAspectRatio: 1, // 宽高比
      // ),
      itemBuilder: _initData,
    );
  }
}
